﻿using Microsoft.Lync.Controls;

namespace LyncHotkeys
{
    using Microsoft.Lync.Model;
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Input;

    /// <summary>
    /// Interaction logic for PopUpWindow.xaml
    /// </summary>
    public partial class PopUpWindow : Window
    {
        private readonly LyncClient lyncClient;
        private Self self;
        private ActionTrigger lastActionTrigger;

        public PopUpWindow()
        {
            InitializeComponent();

            // Screen Location
            Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty) - 30;
            Top = SystemParameters.PrimaryScreenHeight - (double)GetValue(HeightProperty) - 60;

            Loaded += (s, e) =>
            {
                new HotKey(ModifierKeys.Control, Keys.D1, this).HotKeyPressed += (k) => SetStatus(ContactAvailability.Away, s);
                new HotKey(ModifierKeys.Control, Keys.D2, this).HotKeyPressed += (k) => SetStatus(ContactAvailability.Free, s);
                new HotKey(ModifierKeys.Control, Keys.D3, this).HotKeyPressed += (k) => SetStatus(ContactAvailability.Busy, s);
                new HotKey(ModifierKeys.Control, Keys.D4, this).HotKeyPressed += (k) => SetStatus(ContactAvailability.DoNotDisturb, s);
            };

            // Current user info
            lyncClient = LyncClient.GetClient();
            self = lyncClient.Self;

            Presence.Source = self.Contact.Uri;
            Presence.SingleClickAction = PresenceClickActions.ShowContactBrief;
        }

        private void Button_Click_Busy(object sender, RoutedEventArgs e)
        {
            SetStatus(ContactAvailability.Busy, sender);
        }

        private void Button_Click_DoNotDisturb(object sender, RoutedEventArgs e)
        {
            SetStatus(ContactAvailability.DoNotDisturb, sender);
        }

        private void Button_Click_Free(object sender, RoutedEventArgs e)
        {
            SetStatus(ContactAvailability.Free, sender);
        }

        private void Button_Click_Away(object sender, RoutedEventArgs e)
        {
            SetStatus(ContactAvailability.Away, sender);
        }

        private void SetStatus(ContactAvailability contactAvailability, object sender)
        {
            this.WindowState = WindowState.Normal;

            if (sender is System.Windows.Controls.Button)
            {
                this.lastActionTrigger = ActionTrigger.UI;
            }
            else
            {
                this.lastActionTrigger = ActionTrigger.HotKey;
            }


            var newInformation = new Dictionary<PublishableContactInformationType, object>
            {
                {PublishableContactInformationType.Availability, contactAvailability}
            };
            self.BeginPublishContactInformation(newInformation, PublishContactInformationCallback, null);

            //var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(2));
            ////anim.Completed += (s, _) => this.Close();
            //this.BeginAnimation(UIElement.OpacityProperty, anim);
            if (this.lastActionTrigger == ActionTrigger.HotKey)
            {
                Thread.Sleep(4000);
                this.WindowState = WindowState.Minimized;
            }
        }

        private void PublishContactInformationCallback(IAsyncResult ar)
        {
            lyncClient.Self.EndPublishContactInformation(ar);
        }

        //private void Window_StateChanged(object sender, EventArgs e)
        //{
        //    var currentAvailability = (ContactAvailability)self.Contact.GetContactInformation(ContactInformationType.Availability);
        //}

        private void Window_LostFocus(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;

        }
    }
}
